package com.example.take_out_admin.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.take_out_admin.common.Result;
import com.example.take_out_admin.config.JwtConfig;
import com.example.take_out_admin.entity.Employee;
import com.example.take_out_admin.exceptions.BusinessException;
import com.example.take_out_admin.exceptions.enumetation.ResponseEnum;
import com.example.take_out_admin.service.EmployeeService;
import com.example.take_out_admin.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.websocket.server.PathParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 员工信息 前端控制器
 * </p>
 *
 * @author 时月
 * @since 2024-06-27
 */
@RestController
@Slf4j
@RequestMapping("/employee")
public class EmployeeController {
    @Resource
    private EmployeeService employeeService;
    @Resource
    private HttpServletRequest request;
    @Resource
    private JwtConfig jwtConfig;

    @PostMapping("login")
    public Result<String> login(@RequestBody Employee employee){
        String token= employeeService.login(employee);
        return Result.success(token);
    }

    @GetMapping("{id}")
    public Result<Employee> findById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        employee.setPassword(null);
        return Result.success(employee);
    }

    @GetMapping("page")
    public Result<IPage<Employee>> page(int page, int pageSize, String name) {
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(name),Employee::getName, name);
        IPage<Employee> pageInfo = employeeService.page(new Page<>(page, pageSize), wrapper);
        return Result.success(pageInfo);
    }

    @PostMapping()
    public Result<Object> save(@RequestBody Employee employee) {
        employeeService.saveEmployee(employee);
        return Result.success();
    }
    @PutMapping()
    public Result<Object> update(@RequestBody Employee employee) {
        String token = request.getHeader("token");
        log.info("token:{}", token);
        Claims claims = JwtUtils.parseJwt(token, jwtConfig.getKey());
        String name = claims.get("name", String.class);

        employee.setCreateUser(name);
        employee.setUpdateUser(name);

        Date now = new Date();
        employee.setCreateTime(now);
        employee.setUpdateTime(now);
//        this.update(employee);
        employeeService.updateEmployee(employee);
        return Result.success();
    }
    @PostMapping("/status/{status}")
    public Result<Object> updatestuats(@PathVariable("status") Integer status,@PathParam("id") String id) {
        LambdaQueryWrapper<Employee> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getId,id);
        Employee one = employeeService.getOne(wrapper);
        if (one!=null){
            one.setStatus(status);
            System.out.println("获取了用户的信息");
            employeeService.updateEmployee(one);/**/
            System.out.println("修改了用户的信息");
            System.out.println("修改后用户的状态："+one.getStatus());
            one.setPassword(null);
        }else {
            throw new BusinessException(ResponseEnum.USER_NULL);
        }

        return Result.success();
    }

}
